<!--
    Copyright 2010 Justin Lipton, Jonathan Rosenberg
    
    This file is part of MediaList.

    MediaList is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MediaList is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MediaList.  If not, see <http://www.gnu.org/licenses/>.
-->

<?php
$user=$_COOKIE["user"];
$status=$_GET["status"];
$showid=$_GET["showid"];
$prev=$_GET["prev"];

$con = mysql_connect("localhost", "root");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("DB", $con);

$sql="SELECT * FROM Users WHERE Username='".$user."'";
$result=mysql_query($sql, $con);
$row=mysql_fetch_array($result);

if($status!=$prev)
{
    if($status=='W')
    {
        $sql="UPDATE ".$user." SET Status='W' WHERE ShowId='".$showid."'";
        mysql_query($sql, $con);
        
        $sql="UPDATE Users SET NumViewing='".($row["NumViewing"]+1)."' WHERE Username='".$user."'";
        mysql_query($sql, $con);
    }
    else if($status=='P')
    {
        $sql="UPDATE ".$user." SET Status='P' WHERE ShowId='".$showid."'";
        mysql_query($sql, $con);
        
        $sql="UPDATE Users SET NumPlanning='".($row["NumPlanning"]+1)."' WHERE Username='".$user."'";
        mysql_query($sql, $con);
    }
    else if($status=='C')
    {   
        $sql="UPDATE ".$user." SET Progress='".$row["NumEpisodes"]."'";
        mysql_query($sql, $con);
    
        $sql="UPDATE ".$user." SET Status='C' WHERE ShowId='".$showid."'";
        mysql_query($sql, $con);
        
        $sql="UPDATE Users SET NumCompleted='".($row["NumCompleted"]+1)."' WHERE Username='".$user."'";
        mysql_query($sql, $con);
    }
    else if($status=='D')
    {
        $sql="UPDATE ".$user." SET Status='D' WHERE ShowId='".$showid."'";
        mysql_query($sql, $con);
        
        $sql="UPDATE Users SET NumDropped='".($row["NumDropped"]+1)."' WHERE Username='".$user."'";
        mysql_query($sql, $con);
    }
    if($prev=='P')
    {
        $sql="UPDATE Users SET NumPlanning='".($row["NumPlanning"]-1)."' WHERE Username='".$user."'";
        mysql_query($sql, $con);
    }
    else if($prev=='W')
    {
        $sql="UPDATE Users SET NumWatching='".($row["NumPWatching"]-1)."' WHERE Username='".$user."'";
        mysql_query($sql, $con);
    }
    else if($prev=='D')
    {
        $sql="UPDATE Users SET NumDropped='".($row["NumDropped"]-1)."' WHERE Username='".$user."'";
        mysql_query($sql, $con);
    }
    else if($prev=='C')
    {
        $sql="UPDATE Users SET NumCompleted='".($row["NumCompleted"]-1)."' WHERE Username='".$user."'";
        mysql_query($sql, $con);
    }
}

echo "<meta http-equiv=\"REFRESH\" content=\"0;url=table.php?currentUser=".$user."\">";


mysql_close($con);
?>